package com.lc1993929.actions;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by LiuChang on 2017/5/8.
 */
public class ActionMappingManager {

    //保存所有Action的ActionMapping
    private static Map<String, ActionMapping> actionMappings = new HashMap<String, ActionMapping>();

    public ActionMapping getActionMapping(String actionName) throws Exception {
        //根据ActionName获取Mapping
        ActionMapping actionMapping = null;

        //判断actionName是否为空，如果actionName为空，就不用找ActionMapping，因为系统中没有该Action
        if (!(null == actionName || "".equals(actionName))) {
            actionMapping = this.actionMappings.get(actionName);
        }

        if (null == actionMapping) {
            throw new Exception("系统配置文件中找不到：" + actionName + "，请检查配置文件");
        }

        return actionMapping;
    }


    public ActionMappingManager() {
    }

    //加载action配置文件
    public ActionMappingManager(String[] configureFileNmaes) {
        for (String configureFileName : configureFileNmaes) {
            init(configureFileName);//解析xml文件
        }
    }

    //解析加载xml配置文件
    private void init(String configureFileName) {
        //判断文件是否为空
        try {
            if (configureFileName == null) {
                throw new Exception("请检查配置文件！");
            }

            //输入流，用户加载系统的配置文件
            InputStream is = this.getClass().getResourceAsStream("/" + configureFileName);

            //读取xml文件
            Document document = new SAXReader().read(is);
            //获取根节点
            Element root = document.getRootElement();
            //获取Actions节点
            Element actions = (Element) root.elementIterator("actions").next();


            //遍历Action
            for (Iterator<Element> actionIt = actions.elementIterator("action"); actionIt.hasNext(); ) {
                //获取action元素，并将其属性进行封装
                Element actionElement = actionIt.next();
                ActionMapping actionMapping = new ActionMapping();
                actionMapping.setName(actionElement.attributeValue("name"));
                actionMapping.setClassName(actionElement.attributeValue("class"));

                //遍历Action中的子元素
                for (Iterator<Element> result = actionElement.elementIterator("result"); result.hasNext(); ) {

                    Element resultElement = result.next();
                    String resultName = resultElement.attributeValue("name");
                    String resultValue = resultElement.getText();
                    //判断数据是否为空
                    if (null == resultName || "".equals(resultName)) {
                        resultName = "success";
                    }
                    //将每个封装好的result添加到ActionMapping中
                    actionMapping.addResult(resultName, resultValue);

                }
                //将ActionMapping存放到actionMappings中
                actionMappings.put(actionMapping.getName(), actionMapping);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
